/*
 * @Project Name: 11
 * @File Name: AmadoCommentGenerator.java
 * @Package Name: org.mybatis.generator.internal.util
 * @Date: 2017年5月26日下午12:21:43
 * @Creator: wangyulong-910
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package org.amado.generator.internal;

import static org.mybatis.generator.internal.util.StringUtility.isTrue;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.amado.generator.util.DateUtil;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

/**
 * 扩展 mybatis generator 的备注生成
 * @author wangyulong-910
 * @date 2017年5月26日下午12:21:43
 * @see DefaultCommentGenerator
 */
public class CommentGenerator extends DefaultCommentGenerator {

	/** The properties. */
	private Properties properties;
	/** The suppress date. */
	private boolean suppressDate;
	/** The suppress all comments. */
	private boolean suppressAllComments;
	private SimpleDateFormat dateFormat;
	private String author;
	
	Map<String,String> methodNameMap;
	/**
	 * The addition of table remark's comments. If suppressAllComments is true,
	 * this option is ignored
	 */
	private boolean addRemarkComments;
	public CommentGenerator() {
		super();
		properties = new Properties();
		suppressDate = false;
		suppressAllComments = false;
		addRemarkComments = false;
		author = "";
		
		methodNameMap = new HashMap<>();
		methodNameMap.put("deleteById", "删除");
		methodNameMap.put("updateById", "根据ID修改");
		methodNameMap.put("insert", "插入");
		methodNameMap.put("getById", "根据ID查找");
		methodNameMap.put("list", "查询");
	}
	@Override
	public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
		if (suppressAllComments) {
			return;
		}

		/**
	     * 添加保存商品
	     * @date 2017年5月18日上午11:11:31
	     * @author wangyulong-910
	     * @since 1.0.0 
	     * @param list 商品实体列表 
	     **/
		method.addJavaDocLine("/**"); //$NON-NLS-1$\
		String methodName =method.getName();
		if(methodNameMap.containsKey(methodName)){
			method.addJavaDocLine(" * "+methodNameMap.get(methodName)+introspectedTable.getRemarks()); //$NON-NLS-1$
		}else{
			method.addJavaDocLine(" * "); //$NON-NLS-1$
		}
		method.addJavaDocLine(" * @author " + author); //$NON-NLS-1$
		method.addJavaDocLine(" * @date " + DateUtil.getDateStr()); //$NON-NLS-1$
		method.addJavaDocLine(" */"); //$NON-NLS-1$
	}

	@Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
	}

	@Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
	}

	public void addConfigurationProperties(Properties properties) {
		this.properties.putAll(properties);
		suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
		suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
		addRemarkComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
		author = properties.getProperty("author");
		String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
		if (StringUtility.stringHasValue(dateFormatString)) {
			dateFormat = new SimpleDateFormat(dateFormatString);
		}
	}

	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
	}

	public void addComment(XmlElement xmlElement) {
		if (suppressAllComments) {
            return;
        }
		xmlElement.addElement(new TextElement("<!--@author: "+ author +"-->"));
		xmlElement.addElement(new TextElement("<!--@date: "+ DateUtil.getDateStr() +"-->"));
	}

	@Override
	public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		if (suppressAllComments || !addRemarkComments) {
			return;
		}
		topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$
		/**
		 * TODO
		 * @author wangyulong-910
		 * @date 2017年5月26日下午4:14:33
		 */
		String remarks = introspectedTable.getRemarks();
		if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
			String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
			for (String remarkLine : remarkLines) {
				topLevelClass.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
			}
		}
		topLevelClass.addJavaDocLine(" * @author " + author); //$NON-NLS-1$
		topLevelClass.addJavaDocLine(" * @date " + DateUtil.getDateStr()); //$NON-NLS-1$
		topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$
	}

	@Override
	public void addGetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
	}

	@Override
	public void addSetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
	}
}
